package com.pay.familycar.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.pay.familycar.R;
import com.pay.familycar.base.BaseActivity;
import com.pay.familycar.base.BasePresenter;
import com.pay.familycar.presenter.Car_HomePresenter;
import com.pay.familycar.response.NVHPRankResponse;
import com.pay.familycar.view.Car_HomeView;
import com.uuzuche.lib_zxing.activity.CaptureActivity;
import com.uuzuche.lib_zxing.activity.CodeUtils;

/**
 * 首页
 */
public class HomeActivity extends BaseActivity<Car_HomePresenter> implements Car_HomeView {

    private int REQUEST_CODE=333;


    @Override
    protected Car_HomePresenter createPresenter() {
        return new Car_HomePresenter(this);
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_home;
    }

    @Override
    public void initData() {
        super.initData();
        mPresenter.getNVHPRank();
    }

    @Override
    public void onVDHomeHistorystatusSuccess(NVHPRankResponse vdHomeHistoryStatusResponse) {
        System.out.println("请求成功 "+vdHomeHistoryStatusResponse.getStatus()+" "+vdHomeHistoryStatusResponse.getRet().size());
        Intent intent = new Intent(HomeActivity.this, CaptureActivity.class);
        startActivityForResult(intent, REQUEST_CODE);
    }

    @Override
    public void onVDHomeHistorystatusError() {
        System.out.println("请求失败 ");
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        /**
         * 处理二维码扫描结果
         */
        if (requestCode == REQUEST_CODE) {
            //处理扫描结果（在界面上显示）
            if (null != data) {
                Bundle bundle = data.getExtras();
                if (bundle == null) {
                    return;
                }
                if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_SUCCESS) {
                    String result = bundle.getString(CodeUtils.RESULT_STRING);
                    Toast.makeText(this, "解析结果:" + result, Toast.LENGTH_LONG).show();
                    System.out.println("失败了"+result);
                } else if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_FAILED) {
                    Toast.makeText(HomeActivity.this, "解析二维码失败", Toast.LENGTH_LONG).show();
                    System.out.println("失败了成功解析二维码失败");
                }
            }
        }
    }
}
